package chapter07

object Test17_CommonWordCount {
  def main(args: Array[String]): Unit = {
    val strings:List[String] = List(
      "hello",
      "hello world",
      "hello scala",
      "hello spark from scala",
      "hello flink form scala"
    )

    // 首先将所有文字打散，变成单个文字，然后扁平化
    val stringses: List[Array[String]] = strings.map((s) => s.split(" "))
    // 然后扁平化
    val flatten: List[String] = stringses.flatten

    //
    strings.flatMap(_.split(" "))

    // 分组
    val stringToStrings: Map[String, List[String]] =
      flatten.groupBy(w => w)

    // 对分组之后的list取长度
    val stringToInt: Map[String, Int] = stringToStrings.map(kv => (kv._1, kv._2.length))

    // 将map转化为list，并排序取前3
    val tuples: List[(String, Int)] = stringToInt.toList.sortWith(_._2 > _._2).take(3)

  }
}
